<template>
  <view class="wrap">
    <uv-navbar :fixed="false" title="社群分成设置" autoBack safeAreaInsetTop placeholder :border='false'>
      <template v-slot:right>
        <view class="navEditName nav-right" @click="goWeb">规则</view>
      </template>
    </uv-navbar>
    
    <view class="box" v-if="base.owner.length">
      <view class="hd">
        <view class="left">群主</view>
        <view class="right">分成占比：{{ ownerTotal }}%</view>
      </view>
      <view class="list">
        <view class="item" v-for="(item,index) in base.owner" :key="index">
          <view class="user">
            <view class="avatar">
              <image :src="setUrl(item.avatar)" mode="aspectFill" class="img" v-if="item.avatar"></image>
              <image src="../../../static/logo.png" mode="aspectFill" class="img" v-else></image>
            </view>
            <view class="name">{{ item.nickname }}</view>
            <view class="right">
              <view class="tip red">群主</view>
            </view>
          </view>
          <view class="setting">
            <view class="test">
              <uv-input type="text" v-model="item.ratio" placeholder="请输入比例" :maxlength='11' border="surround" autoBlur fontSize='28rpx' color='#000000' placeholderStyle='font-size: 28rpx; color: #B8B8B8' :customStyle="{padding: '4px'}"></uv-input>
            </view>
            <view class="font">%</view>
          </view>
        </view>
      </view>
    </view>
    <view class="box" v-if="base.partner.length">
      <view class="hd">
        <view class="left">合伙人</view>
        <view class="right">分成占比：{{ partnerTotal }}%</view>
      </view>
      <view class="list">
        <view class="item" v-for="(item,index) in base.partner" :key="index">
          <view class="user">
            <view class="avatar">
              <image :src="setUrl(item.avatar)" mode="aspectFill" class="img" v-if="item.avatar"></image>
              <image src="../../../static/logo.png" mode="aspectFill" class="img" v-else></image>
            </view>
            <view class="name">{{ item.nickname }}</view>
            <view class="right">
              <view class="tip yellow">合伙人</view>
            </view>
          </view>
          <view class="setting">
            <view class="test">
              <uv-input type="text" v-model="item.ratio" placeholder="请输入比例" :maxlength='11' border="surround" autoBlur fontSize='28rpx' color='#000000' placeholderStyle='font-size: 28rpx; color: #B8B8B8' :customStyle="{padding: '4px'}"></uv-input>
            </view>
            <view class="font">%</view>
          </view>
        </view>
      </view>
    </view>
    <view class="box" v-if="base.friends.length">
      <view class="hd">
        <view class="left">嘉宾</view>
        <view class="right">分成占比：{{ friendsTotal }}%</view>
      </view>
      <view class="list">
        <view class="item" v-for="(item,index) in base.friends" :key="index">
          <view class="user">
            <view class="avatar">
              <image :src="setUrl(item.avatar)" mode="aspectFill" class="img" v-if="item.avatar"></image>
              <image src="../../../static/logo.png" mode="aspectFill" class="img" v-else></image>
            </view>
            <view class="name">{{ item.nickname }}</view>
            <view class="right">
              <view class="tip blue">嘉宾</view>
            </view>
          </view>
          <view class="setting">
            <view class="test">
              <uv-input type="text" v-model="item.ratio" placeholder="请输入比例" :maxlength='11' border="surround" autoBlur fontSize='28rpx' color='#000000' placeholderStyle='font-size: 28rpx; color: #B8B8B8' :customStyle="{padding: '4px'}"></uv-input>
            </view>
            <view class="font">%</view>
          </view>
        </view>
      </view>
    </view>
    <view class="footer">
      <button class="btn" hover-class="none" @click="keyUp">保存</button>
      <uv-safe-bottom></uv-safe-bottom>
    </view>
    <view class="safe"></view>
    <uv-safe-bottom></uv-safe-bottom>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useOrder } from '@/store';
import { post } from '@/api';
import { onShow } from '@dcloudio/uni-app';
import { add, toast, setUrl } from '@/common';

let base = ref({
  owner: [],
  partner: [],
  friends: []
});

const ownerTotal = computed(() => {
  let num = 0;
  base.value.owner.forEach(e => {
    num = add(num, e.ratio);
  })
  return num;
})
const partnerTotal = computed(() => {
  let num = 0;
  base.value.partner.forEach(e => {
    num = add(num, e.ratio);
  })
  return num;
})
const friendsTotal = computed(() => {
  let num = 0;
  base.value.friends.forEach(e => {
    num = add(num, e.ratio);
  })
  return num;
})
const totalNumber = computed(() => {
  let num = 0;
  let n1 = add(ownerTotal.value, partnerTotal.value);
  let n2 = add(n1, friendsTotal.value);
  return n2;
})

const body = computed(() => {
  const store = useOrder();
  return uni.$uv.deepClone(store.group);
});
onShow(() => {
  base.value = {
    owner: [],
    partner: [],
    friends: []
  };
  getData();
})

function keyUp(){
  if(totalNumber.value > 100) return toast("总比例不能超过100%");
  let arr = [];
  base.value.owner.forEach(e => {
    arr.push({ user_id: e.user_id, ratio: e.ratio })
  })
  base.value.partner.forEach(e => {
    arr.push({ user_id: e.user_id, ratio: e.ratio })
  })
  base.value.friends.forEach(e => {
    arr.push({ user_id: e.user_id, ratio: e.ratio })
  })
  const params = {
    association_id: body.value.id,
    ratio_arr: arr
  }
  post('api/association/allocationRatioConfig', params).then(res => {
    if(res.code == 1){
      toast('设置成功', true);
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000)
    }
  })
}
function getData(){
  const params = {
    page: 1,
    limit: 100,
    association_id: body.value.id
  }
  post('api/association_member/getAssociationMemberList', params).then(res => {
    if(res.code == 1){
      const { data } = res.data;
      data.forEach(e => {
        e.ratio = Number(e.ratio);
        if(e.identity == 1){
          base.value.owner.push(e)
        }
        if(e.identity == 2){
          base.value.partner.push(e)
        }
        if(e.identity == 3){
          base.value.friends.push(e)
        }
      })
    }
  })
}
function goWeb(){
  uni.navigateTo({
    url: '/pages/web?id=11'
  });
}
</script>

<style lang="scss" scoped>
  .safe{
    width: 100%;
    height: 208rpx;
    background-color: #F5F5F5;
  }
  .footer{
    width: 100%;
    background-color: #fff;
    padding: 0 30rpx;
    padding-top: 16rpx;
    padding-bottom: 30rpx;
    position: fixed;
    left: 0;
    bottom: 0;
  }
  .box{
    .list{
      .item{
        width: 100%;
        height: 150rpx;
        background-color: #fff;
        padding: 0 30rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .user{
          flex: 1;
          width: 0;
          padding-right: 30rpx;
          display: flex;
          align-items: center;
          .right{
            width: 100rpx;
            .tip{
              font-weight: 400;
              font-size: 20rpx;
              color: #FFFFFF;
              height: 32rpx;
              border-radius: 32rpx;
              line-height: 32rpx;
              padding: 0 12rpx;
              display: inline-block;
              &.red{
                background-color: #E43222;
              }
              &.yellow{
                background-color: #F0BA16;
              }
              &.blue{
                background-color: #465B89;
              }
            }
          }
          .name{
            max-width: 260rpx;
            padding-right: 8rpx;
            padding-left: 20rpx;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .avatar{
            width: 70rpx;
            height: 70rpx;
            overflow: hidden;
            border-radius: 50%;
          }
        }
        .setting{
          width: 230rpx;
          display: flex;
          align-items: center;
          .font{
            font-weight: 500;
            font-size: 28rpx;
            color: #737373;
            padding-left: 20rpx;
          }
        }
      }
    }
    .hd{
      width: 100%;
      height: 90rpx;
      padding: 0 30rpx;
      font-weight: 500;
      font-size: 28rpx;
      color: #737373;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
  .navEditName{
    font-weight: 400;
    font-size: 28rpx;
    &.nav-right{
      color: #E43222;
    }
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
  }
</style>